package com.example.demovolley;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
       final String TAG = "MainActivity";
       TextView txt_Result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        txt_Result = findViewById(R.id.txt_view);

              findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      useVolleStringRequest();
                  }
              });
              findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      useVolleStringPost("13688888888");
                  }
              });
              findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      useVolleyJsonRequest();
                  }
              });
    }

    private void useVolleyJsonRequest() {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String url = "http://t.weather.sojson.com/api/weather/city/101280506";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
             Log.e(TAG,jsonObject.toString());

                try {
                    String time = jsonObject.getString("time");
                    String city = jsonObject.getJSONObject("cityInfo").getString("city");
                    String pm25 = jsonObject.getJSONObject("data").getString("pm25");
                    String wendu = jsonObject.getJSONObject("data").getString("wendu");
                    txt_Result.setText(time+" "+city+" "+pm25+" "+wendu);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }

        });
        requestQueue.add(jsonObjectRequest);
    }

    private void useVolleStringPost(final String phone) {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

        String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
               txt_Result.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        })
        {
            protected Map<String,String> getParams  (){
                Map<String,String> map = new HashMap<>();
                map.put("mobileCode",phone);
                map.put("userID","");
                return map;
            }

        };

        requestQueue.add(stringRequest);
    }

    private void useVolleStringRequest() {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

        String url = "http://jsxy.gdcp.cn/DocHtml/2/2012/5/6/288865442197.html";

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG,s);

                txt_Result.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        });

     requestQueue.add(stringRequest);
     }

}
